using System;
using Sce.PlayStation.Core;

namespace Nyanimals
{
	public class CPlayerPickupState : CPlayerBaseState
	{
		public CPlayerPickupState(string szName, CPlayer pl) : base(szName, pl)
		{
		}

		public override void Enter ()
		{
			player.ChangeAnimation("pickup");
			// Console.WriteLine("PlayerPickupState::Enter()");
		}

		public override void Exit ()
		{
			// Console.WriteLine("PlayerPickUpState::Exit()");
		}
        public override void Input ()
		{
			CInput input = CInput.Instance;			
		}


     
		public override void Update (float deltaTime)
		{
			if(player.Context.Playing == false)
			{
				owningMachine.ChangeState("idle");
                
                if(player.HeldObject != null)
                {
                    // clamp to "ground"
                    Vector3 pos = player.HeldObject.Pivot;
                    pos.Y = 0.0f;
                    player.HeldObject.Pivot = pos;
                    
                    // drop the object
                    switch(player.HeldObject.Type)
                    {
                    case ObjectTypes.Nyanimal_Object:
                        {
                            CNyanimal nyan = (CNyanimal)player.HeldObject;
                            nyan.StateMachine.ChangeState("idle");
                            nyan.Held = false;
                            
                        } break;
                    case ObjectTypes.Food_Object:
                        {
                            CBaseFood food = (CBaseFood)player.HeldObject;
                            food.ObjectHolding = null;
                                                    
                        } break;
                    }
                    player.HeldObject = null;
                    
                    // tell the display to not show anymore
                    CEventManager.Instance.QueueEvent(new CPickupObjectEvent(null));
                }
			}
			
			player.Velocity = Vector3.Zero;
			player.LeftStick = Vector2.Zero;
		}
        
        public bool PickUp(ref CNyanimal nyan)
        {
            // if ho96lding a nyanimal, leave
            if(player.HeldObject == null)
            {
                // check if between the pickup frames
                if( player.Context.AfterFrame(215))
                {
                    return true;
                }
            }
            return false;
        }
        public bool PickUp(ref CBaseFood food)
        {
            // if holding a nyanimal, leave
            if(player.HeldObject == null)
            {
                // check if between the pickup frames
                if( player.Context.AfterFrame(215))
                {
                    return true;
                }
            }
            return false;
        }
	}
}

